//
//  PersonInfoViewController.m
//  51-Tour
//
//  Created by xiang ying on 14-3-23.
//  Copyright (c) 2014年 xiang ying. All rights reserved.
//

#import "PersonInfoViewController.h"
#import "NSString+TPCategory.h"
#import "UIColor+TPCategory.h"
#import "ServiceManage.h"
#import "SVProgressHUD.h"
#import "UserDTO.h"
#import "TSLocateView.h"
#import "NSString+TPCategory.h"
#import "CityViewController.h"


#define CELL_HEIGHT 44
#define SECTION_HEIGHT  30

@interface PersonInfoViewController ()<UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate,UITableViewDataSource,UITableViewDelegate>{
    NSArray         *_itemsArray;
    
    
    NSArray         *_lanArray,*_lanKeyArray;
    CGFloat contentHeight ;
    
    
    NSInteger       selectIndex;    //针对照片和导游证上传的tag
    
    UserDTO         *_modifyUser;
    
    UIView          *_languageView;
    UITableView     *_chooseMenu;
    
    NSMutableArray  *_chooselanguageArray;
}


@end

@implementation PersonInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateIcon:) name:@"ICONCHANGE" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateIcon:) name:@"WORKICONCHANGE" object:nil];
    }
    return self;
}

- (void)initNavigationItme:(NSString*)title
{
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 320, 44)];
    titleView.backgroundColor = [UIColor clearColor];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"topbar-bg_02"]];
    imageView.frame = CGRectMake(-8, 0, 320, 44);
    imageView.userInteractionEnabled = YES;
    [titleView addSubview:imageView];
    
    UIButton *leftbtn = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 44, 44)];
    [leftbtn setImage:[UIImage imageNamed:@"ic-back_02"] forState:UIControlStateNormal];
    [imageView addSubview:leftbtn];
    [leftbtn addTarget:self action:@selector(popself) forControlEvents:UIControlEventTouchUpInside];
    
    //拓展
    UIButton *rightBtn = [[UIButton alloc] initWithFrame: CGRectMake(320-44, 0, 44, 44)];
    [rightBtn setImage:[UIImage imageNamed:@"ic-submit_03"] forState:UIControlStateNormal];
    [imageView addSubview:rightBtn];
    [rightBtn addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 7, 200, 35)];
    [titleLabel setText:title];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [titleLabel setTextColor:THEMECOLOR];
    titleLabel.backgroundColor = [UIColor clearColor];
    [imageView addSubview:titleLabel];
    
    [self.navigationItem setHidesBackButton:YES];
    self.navigationItem.titleView =  titleView;
}

- (void)submit{
    //submit feedback
    if([self checkInput]){
        [SVProgressHUD showWithStatus:[[DataCenter instance] getStringForKey:@"loading"]];
        NSDictionary *dic = @{@"Mobile": [DataCenter instance].user.userPhone,
                              @"UserName":[self getInputbyTag:1].text,
                              @"Sex":[NSString stringWithFormat:@"%d",_modifyUser.sex],
                              @"Icon":_modifyUser.userIconPath,
                              @"GuideNum":[self getInputbyTag:11].text,
                              @"GuideIcon":_modifyUser.GuideIconPath,
                              @"GuideLan":_modifyUser.GuideLan,
                              @"GuideWorkYear":[self getInputbyTag:14].text,
                              @"GuideCity":_modifyUser.GuideCity,
                              @"CardBank":[self getInputbyTag:21].text,
                              @"CardPerson":[self getInputbyTag:22].text,
                              @"CardNum":[self getInputbyTag:23].text};
        NSLog(@"dic====%@",dic);
        [ServiceManage modifyRequestUserInfo:dic completion:^(RespondDTO *respond) {
            NSLog(@"respond====%@",respond.msg);
            if (respond.flag == respond_success) {
                [[DataCenter instance].user copyfrom:_modifyUser];
                [SVProgressHUD showSuccessWithStatus:[[DataCenter instance] getStringForKey:@"success"]];
                [self popself];
            }else{
                [SVProgressHUD showErrorWithStatus:[[DataCenter instance] getStringForKey:@"failed"]];
            }
        }];
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getCity:) name:@"选中城市" object:nil];
}
-(void)getCity:(NSNotification *)sender
{
    NSMutableArray *tempArray =[sender object];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _modifyUser = [[UserDTO alloc] init];
    [_modifyUser copyfrom:[DataCenter instance].user];
    
    [self initNavigationItme:[[DataCenter instance] getStringForKey:@"PersonInfo"]];
    
    [self.contentView setShowsVerticalScrollIndicator:NO];
    _itemsArray =   @[@[[[DataCenter instance] getStringForKey:@"Name"],[[DataCenter instance] getStringForKey:@"Sex"],[[DataCenter instance] getStringForKey:@"profileIcon"]],
                      @[[[DataCenter instance] getStringForKey:@"tgNum"],[[DataCenter instance] getStringForKey:@"tgPhoto"],[[DataCenter instance] getStringForKey:@"tgLan"],[[DataCenter instance] getStringForKey:@"tgYear"],[[DataCenter instance] getStringForKey:@"GuideCity"]],
                      @[[[DataCenter instance] getStringForKey:@"card_bank"],[[DataCenter instance] getStringForKey:@"card_user"],[[DataCenter instance] getStringForKey:@"card_number"]]];
    
//    _lanArray = @[@"中文",@"英语",@"日语",@"韩语",@"法语"];
    
    _lanKeyArray = @[@"zh-cn",@"en-us",@"ja-jp",@"ko-kr",@"fr-fr"];
    _lanArray = @[[[DataCenter instance] getStringForKey:@"zh-cn"],
                  [[DataCenter instance] getStringForKey:@"en-us"],
                  [[DataCenter instance] getStringForKey:@"ja-jp"],
                  [[DataCenter instance] getStringForKey:@"ko-kr"],
                  [[DataCenter instance] getStringForKey:@"fr-fr"]];
    
    _chooselanguageArray = [NSMutableArray array];
    
    NSArray *array = [_modifyUser.GuideLan componentsSeparatedByString:@","];
    if (array) {
        for (NSString *str in array) {
            if ([_lanKeyArray containsObject:str]) {
                [_chooselanguageArray addObject:str];
            }
        }
    }
    //section
    CGFloat height = 0;
    
    for (int j = 0; j<3; j++) {
//        height = [self viewForHeaderInSection:j height:height];
        height = height + SECTION_HEIGHT;
        for (int i = 0; i<[_itemsArray[j] count]; i++) {
            height = [self cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j] height:height];
        }
    }

    UIButton *confim = [UIButton buttonWithType:UIButtonTypeCustom];
    [confim setTitle:[[DataCenter instance] getStringForKey:@"Submit"] forState:UIControlStateNormal];
    [confim setFrame:CGRectMake(10, height+SECTION_HEIGHT, 300, 40)];
    [confim setBackgroundColor:[UIColor orangeColor]];
    [confim addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:confim];
    
    height = confim.frame.origin.y + confim.frame.size.height;
    
    contentHeight = height+SECTION_HEIGHT;
    [self.contentView setContentSize:CGSizeMake(320, contentHeight)];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self.contentView addGestureRecognizer:tap];
    
    [SVProgressHUD showWithStatus:[[DataCenter instance] getStringForKey:@"loading"]];
    [ServiceManage getRequestUserInfo:@{@"Mobile": [DataCenter instance].user.userPhone} completion:^(RespondDTO *respond) {
        if (respond.flag == respond_success) {
            [SVProgressHUD dismiss];
            [_modifyUser copyfrom:[DataCenter instance].user];
            NSArray *array = [_modifyUser.GuideLan componentsSeparatedByString:@","];
            if (array) {
                [_chooselanguageArray removeAllObjects];
                for (NSString *str in array) {
                    if ([_lanKeyArray containsObject:str]) {
                        [_chooselanguageArray addObject:str];
                    }
                }
            }
            for (int j = 0; j<3; j++) {
                for (int i = 0; i<[_itemsArray[j] count]; i++) {
                    [self setTextFiled:[self getInputbyTag:10*j+i+1]];
                }
            }
        }else{
            [SVProgressHUD showErrorWithStatus:TIP_ERROR];
        }
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)popself{
    [self.navigationController popViewControllerAnimated:NO];
    [[UIManagement shareInstance] showRight];
}

#pragma mark - UI Draw

- (CGFloat)viewForHeaderInSection:(NSInteger)section height:(CGFloat)height{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, height, 320, SECTION_HEIGHT)];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:15];
    label.textAlignment = NSTextAlignmentCenter;
    
    if (section == 0) {
        label.text = @"基本资料";
    }else if(section == 1){
        label.text = @"职业信息";
    }else if (section == 2) {
        label.text = @"添加银行卡";
    }
    [self.contentView addSubview:label];
    
    return height+SECTION_HEIGHT;
}



- (CGFloat)cellForRowAtIndexPath:(NSIndexPath *)indexPath height:(CGFloat)height{

    UIView *cell = [[UIView alloc] initWithFrame:CGRectMake(10, height, 300, CELL_HEIGHT)];
    cell.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:cell];

    UILabel  *label = nil;
    CGFloat offset = 0;
    if (indexPath.section == 0&&indexPath.row == 0) {
        label = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 10, CELL_HEIGHT)];
        label.text = @"*";
        label.textColor = [UIColor redColor];
        [cell addSubview:label];
    }
    
    UILabel *textLabel = nil;
    if (label) {
        textLabel = [[UILabel alloc] initWithFrame:CGRectMake(label.frame.size.width+label.frame.origin.x, 0, 200, CELL_HEIGHT)];
        offset = label.frame.size.width;
    }else{
        textLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 200, CELL_HEIGHT)];
    }
    
    textLabel.textColor = THEMECOLOR;
    textLabel.backgroundColor = [UIColor clearColor];
    textLabel.font = [UIFont systemFontOfSize:14];
    [cell addSubview:textLabel];
    
    UITextField *textInput = [[UITextField alloc] initWithFrame:CGRectZero];
    [textInput addTarget:self action:@selector(textBegin:) forControlEvents:UIControlEventEditingDidBegin];
    [textInput addTarget:self action:@selector(textEnd:) forControlEvents:UIControlEventEditingDidEnd];
    textInput.textColor = [UIColor blackColor];
    textInput.delegate = self;
    textInput.text = @"";
    textInput.font = [UIFont systemFontOfSize:14];
    textInput.tag = indexPath.section*10+indexPath.row+1;
    
    NSLog(@"tag:%d",textInput.tag);
    [cell addSubview:textInput];
    
    textLabel.text = _itemsArray[indexPath.section][indexPath.row];
    CGFloat width = [textLabel.text widthwithfont:textLabel.font height:CELL_HEIGHT]+offset;
    textInput.frame = CGRectMake(width+10, (CELL_HEIGHT-30)/2, cell.frame.size.width-width-20-offset, 30);
    
    UIImageView *_line = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"list-line_14"]];
    _line.frame = CGRectMake(0, CELL_HEIGHT-1, cell.frame.size.width, 1);
    [cell addSubview:_line];
    
    [self setTextFiled:textInput];

    return height+CELL_HEIGHT;
}

-(void)showLanguage:(BOOL)show{
    if (!_languageView) {
        _languageView = [[UIView alloc] initWithFrame:self.view.window.bounds];
        _languageView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
        
        _chooseMenu = [[UITableView alloc] initWithFrame:CGRectMake(30, (self.view.window.frame.size.height-280)/2, 260, 280)];
        _chooseMenu.backgroundColor = [UIColor whiteColor];
        _chooseMenu.dataSource = self;
        _chooseMenu.scrollEnabled = NO;
        _chooseMenu.delegate = self;
        _chooseMenu.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_languageView addSubview:_chooseMenu];
        _languageView.alpha = 0;
    }
    if (show) {
        [self.view.window addSubview:_languageView];
        [UIView animateWithDuration:0.5 animations:^{
            _languageView.alpha = 1.0;
        }];
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            _languageView.alpha = 0;
        } completion:^(BOOL finished) {
            [_languageView removeFromSuperview];
        }];
        
        _modifyUser.GuideLan = @"";
        for (int i = 0 ;i<_chooselanguageArray.count;i++) {
            _modifyUser.GuideLan = [_modifyUser.GuideLan stringByAppendingString:_chooselanguageArray[i]];
            if (i!=_chooselanguageArray.count-1) {
                _modifyUser.GuideLan = [_modifyUser.GuideLan stringByAppendingString:@","];
            }
        }
        [self setTextFiled:[self getInputbyTag:13]];
    }
}

-(void)languageChoose:(id)sender{

    [self showLanguage:NO];
}

#pragma mark - UITableViewDataSource & UITableViewDelegate

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = THEMECOLOR;
    label.textAlignment = NSTextAlignmentCenter;
    label.text = [[DataCenter instance] getStringForKey:@"myprofile_please_choose_language"];//@"请选择语言";
    return label;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:[[DataCenter instance] getStringForKey:@"Sure"] forState:UIControlStateNormal];
    [button setTitleColor:THEMECOLOR forState:UIControlStateNormal];
    [button addTarget:self action:@selector(languageChoose:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _lanArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        cell.backgroundColor = [UIColor clearColor];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        UIImageView *_line = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"list-line_14"]];
        _line.tag = 44;
        _line.frame = CGRectMake(5, 39, 310, 1);
        [cell addSubview:_line];
    }
    
    if ([_chooselanguageArray containsObject:_lanKeyArray[indexPath.row]]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    cell.textLabel.text = _lanArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType =UITableViewCellAccessoryCheckmark;
        [_chooselanguageArray addObject:_lanKeyArray[indexPath.row]];
    }else{
        cell.accessoryType =UITableViewCellAccessoryNone;
        [_chooselanguageArray removeObject:_lanKeyArray[indexPath.row]];
    }
}


#pragma mark - UIImagePickerControllerDelegate
- (void)changeImage:(UITextField*)sender{
    UIActionSheet *actionSheet = nil;
    
    selectIndex = sender.tag;
    if (![UIImagePickerController isSourceTypeAvailable:
          UIImagePickerControllerSourceTypeCamera])
    {
        actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                  delegate:self
                                         cancelButtonTitle:[[DataCenter instance] getStringForKey:@"Cancel"]
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:[[DataCenter instance] getStringForKey:@"choosePhoto"],nil
                       ];
        actionSheet.tag = 1;//表示没有摄像头
    }
    else {
        actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                  delegate:self
                                         cancelButtonTitle:[[DataCenter instance] getStringForKey:@"Cancel"]
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:[[DataCenter instance] getStringForKey:@"takePhoto"],[[DataCenter instance] getStringForKey:@"choosePhoto"],nil
                       ];
        actionSheet.tag = 2;//表示有摄像头
    }
    
    [actionSheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if(actionSheet.tag ==1){
        if (buttonIndex == 0){
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:picker animated:YES completion:nil];
        }
    }
    else if(actionSheet.tag == 2){
        if (buttonIndex == 0) {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:picker animated:YES completion:nil];
            
        }else if (buttonIndex == 1){
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:picker animated:YES completion:nil];
        }
    }else if(actionSheet.tag==3000){
        TSLocateView *locateView = (TSLocateView *)actionSheet;
        TSLocation *location = locateView.locate;
        NSLog(@"city:%@ lat:%f lon:%f", location.city, location.latitude, location.longitude);
        //You can uses location to your application.
        if(buttonIndex == 0) {
            NSLog(@"Cancel");
        }else {
            NSLog(@"Select");
            [[self getInputbyTag:15]setText:location.city];
            NSString *CityStr=[location.city stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            _modifyUser.GuideCity=CityStr;
        }


    }
    else{
        //选择性别
        if (buttonIndex == 0) {
            //男
            [[self getInputbyTag:2] setText:[[DataCenter instance] getStringForKey:@"myprofile_male"]];
            _modifyUser.sex = 1;
        }else if(buttonIndex == 1){
            //女
            [[self getInputbyTag:2] setText:[[DataCenter instance] getStringForKey:@"myprofile_female"]];
            _modifyUser.sex = 0;
        }
    }
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [SVProgressHUD showWithStatus:[[DataCenter instance] getStringForKey:@"myprofile_uploading"]];

    [self dismissViewControllerAnimated:YES completion:^{
        [ServiceManage uploadPic:UIImagePNGRepresentation(image) :^(RespondDTO *respond) {
            if (respond.flag == respond_success) {
                [SVProgressHUD showSuccessWithStatus:[[DataCenter instance] getStringForKey:@"myprofile_upload_success"]];
                //照片
                //导游证
                UITextField *textfield = [self getInputbyTag:selectIndex];
                [textfield setBackground:image];
                textfield.text = @"";
                if (selectIndex == 3) {
                    _modifyUser.userIconPath = respond.data;
                    _modifyUser.userIcon = image;
                }else{
                    _modifyUser.GuideIcon = image;
                    _modifyUser.GuideIconPath = respond.data;
                }
            }else{
                [SVProgressHUD showErrorWithStatus:[[DataCenter instance] getStringForKey:@"myprofile_upload_failure"]];
            }
        }];

    }];
}

#pragma mark - UI set

-(void)textBegin:(UITextField*)textField{
    self.contentView.contentSize = CGSizeMake(320, contentHeight+220);
    CGPoint point = [textField convertPoint:textField.frame.origin toView:self.contentView];
    [self.contentView scrollRectToVisible:CGRectMake(0, point.y-CELL_HEIGHT, 320, self.contentView.frame.size.height) animated:YES];
}

-(void)textEnd:(UITextField*)textField{
    
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    switch (textField.tag) {
        case 2:{
            //性别
            [self tap:nil];
            UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                                             delegate:self
                                                                    cancelButtonTitle:[[DataCenter instance] getStringForKey:@"cancel"]
                                                               destructiveButtonTitle:nil
                                                                    otherButtonTitles:[[DataCenter instance] getStringForKey:@"myprofile_male"],[[DataCenter instance] getStringForKey:@"myprofile_female"],nil
                                                  ];
            actionSheet.tag = 3;
            [actionSheet showInView:self.view];
            return NO;
        }
        case 15:{
//            TSLocateView *locateView = [[TSLocateView alloc] initWithTitle:@"导游所在城市" delegate:self];
//            locateView.tag=3000;
//            [locateView showInView:self.view];
            CityViewController *cv=[[CityViewController alloc]init];
            [self presentViewController:cv animated:YES completion:nil];
            
        

            return NO;
        }
        case 3://照片
        case 12:{//导游证照片
            [self tap:nil];
            [self changeImage:textField];
            return NO;
        }
        case 13:{
            [self tap:nil];
            [self showLanguage:YES];
            return NO;
        }
        default:
            break;
    }
    
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//    if (textField.tag == 1||textField.tag==22) {
//        //姓名
//        if (range.location>10) {
//            return NO;
//        }
//    }else if(textField.tag == 23){
//        if (range.location>18) {
//            //19位银行卡号
//            return NO;
//        }
//    }else if(textField.tag == 14){
//        if (range.location>1) {
//            //2位工作年限
//            return NO;
//        }
//    }else if(textField.tag == 11||textField.tag == 21){
//        if (range.location>20) {
//            //21位导游证编号   开户银行
//            return NO;
//        }
//    }
    
    return YES;
}

- (void)tap:(UIGestureRecognizer*)gesture{
    [self.view endEditing:YES];
    self.contentView.contentSize = CGSizeMake(320, contentHeight);
}

-(BOOL)checkInput{
    
    BOOL right = YES;
    NSString *msg = nil;
    if ([[self getInputbyTag:1].text isEqualToString:@""]) {
        //姓名
        msg = [[DataCenter instance] getStringForKey:@"myprofile_empty_name_toast"];//@"姓名不为空";
        right = NO;
    }else {
    
        UITextField *t1 = [self getInputbyTag:21];
        UITextField *t2 = [self getInputbyTag:22];
        UITextField *t3 = [self getInputbyTag:23];

        if (([t1.text isEqualToString:@""]&&[t2.text isEqualToString:@""]&&[t3.text isEqualToString:@""])||(![t1.text isEqualToString:@""]&&![t2.text isEqualToString:@""]&&![t3.text isEqualToString:@""])) {
            right = YES;
        }else{
            if ([t1.text isEqualToString:@""]) {
                msg = [[DataCenter instance] getStringForKey:@"myprofile_empty_bank_toast"];
            }else if([t2.text isEqualToString:@""]){
                msg = [[DataCenter instance] getStringForKey:@"myprofile_empty_bank_user_toast"];
            }else if([t3.text isEqualToString:@""]){
                msg = [[DataCenter instance] getStringForKey:@"myprofile_empty_bank_card_num_toast"];
            }
            right = NO;
        }
    }
    if (!right) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[[DataCenter instance] getStringForKey:@"Tips"] message:msg delegate:nil cancelButtonTitle:[[DataCenter instance] getStringForKey:@"Sure"] otherButtonTitles: nil];
        [alert show];
    }
    return right;
}

-(void)updateIcon:(NSNotification*)noti{
    //来自后面
    if ([[noti object] isEqualToString:@"ICONCHANGE"]) {
        _modifyUser.userIcon = [DataCenter instance].user.userIcon;
        [self performSelectorOnMainThread:@selector(setTextFiled:) withObject:[self getInputbyTag:3] waitUntilDone:YES];
    }else{
        _modifyUser.GuideIcon = [DataCenter instance].user.GuideIcon;
        [self performSelectorOnMainThread:@selector(setTextFiled:) withObject:[self getInputbyTag:12] waitUntilDone:YES];
    }
}

-(void)setTextFiled:(UITextField*)textField{
    switch (textField.tag) {
        case 1:{
            textField.text = _modifyUser.userName;
            textField.placeholder = @"李敏镐";
            break;
        }
        case 2:{
            textField.text = _modifyUser.sex==1?[[DataCenter instance] getStringForKey:@"myprofile_male"]:[[DataCenter instance] getStringForKey:@"myprofile_female"];
            break;
        }
        case 3:{
            textField.frame = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, 50, textField.frame.size.height);
            if (_modifyUser.userIcon) {
                textField.background = _modifyUser.userIcon;
//                textField.text = @"";
            }else if(_modifyUser.userIconPath){
//                textField.text = @"载入中...";
            }else{
//                textField.text = @"未上传";
            }
            textField.textColor = [UIColor greenColor];
            break;
        }
        case 11:{    //导游证编号
            textField.placeholder = @"D-132-23422";
            textField.keyboardType = UIKeyboardTypeASCIICapable;
            textField.text = _modifyUser.tourNum;
            break;
        }
        case 12:{
            textField.frame = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, 50, textField.frame.size.height);
            if (_modifyUser.GuideIcon) {
                textField.background = _modifyUser.GuideIcon;
//                textField.text = @"";
            }else if(_modifyUser.GuideIconPath){
//                textField.text = @"载入中...";
            }else{
//                textField.text = @"未上传";
            }
            textField.textColor = [UIColor greenColor];
            break;
        }
        case 13:{   //语种
            textField.text = @"";
            for (int i = 0; i<_chooselanguageArray.count; i++) {
                textField.text = [textField.text stringByAppendingString:[[DataCenter instance] getStringForKey:_chooselanguageArray[i]]];
                if (_chooselanguageArray.count-1!=i) {
                    textField.text = [textField.text stringByAppendingString:@","];
                }
            }
            break;
        }
        case 14: {   //导游工作年限
            textField.placeholder = @"5";
            textField.keyboardType = UIKeyboardTypeNumberPad;
            textField.text = _modifyUser.tourExp;
            break;
        }
        case 15:{
            textField.text=_modifyUser.GuideCity;
            break;
        }
        case 21:   { //开户银行
            textField.text = _modifyUser.card.cardPlace;
            textField.placeholder = @"工商银行南京分行鼓楼支行";
            break;
        }
        case 22:   { //持卡人姓名
            textField.text = _modifyUser.card.cardPerson;
            break;
        }
        case 23:   { //银行卡号
            textField.keyboardType = UIKeyboardTypeNumberPad;
            textField.text = _modifyUser.card.cardNum;
            textField.placeholder = @"95588025387584888";
            break;
        }
        default:
            break;
    }
}

-(UITextField*)getInputbyTag:(int)tag{
    return (UITextField*)[self.contentView viewWithTag:tag];
}

@end
